<?php

class CParserController extends Controller_Action
{
    public function preDispatch() 
    {
    }
        
    public function getPostsAction()
    {
        $this->_disableLayout();
        if (!$this->_getParam('group_url')) {
            throw new Zend_Controller_Action_Exception('Не указан адресс группы');
        }
        $posts_count = ($this->_getParam('posts_count')) ? $this->_getParam('posts_count') : 20;
        $posts_offset = ($this->_getParam('posts_offset')) ? $this->_getParam('posts_offset') : 0;
        $likes_min = (!$this->_getParam('likes_min') || $this->_getParam('likes_min') == 0) ? 0 : $this->_getParam('likes_min');
        $commerical = ($this->_getParam('commerical')) ? $this->_getParam('commerical') : false;
        $user = $this->getVkUser();
        
        $groupId = $this->_getParam('group_url');
		
		$groupId = strtr($groupId, array(
			'vk.com' => '', 
			'http://' => '',
			'public' => '',
			'club' => ''
		));
		
		$groupId = trim($groupId, '/');
        
        $query = array(
            'gid' => $groupId,
            'access_token' => $user->getAccessToken()
        );
        
        $group = $this->_vk->api('groups.getById', $query);
		$this->_log->info('Parse community get posts: vk api request (groups.getById) params (' . $this->_vk->getStrParams() . '); response=' . $this->_vk->getStrResponse());
        
        $query = array(
            'owner_id' => '-' . $group[0]['gid'],
            'offset' => $posts_offset,
            'count' => $posts_count,
            'filter' => 'all',
            'access_token' => $user->getAccessToken()
        );
        
        $posts = $this->_vk->api('wall.get', $query);
		$this->_log->info('Parse community get posts: vk api request (groups.getById) params (' . $this->_vk->getStrParams() . '); response=' . $this->_vk->getStrResponse());
        $result = array();
		foreach($posts as $post) {
			if (is_int($post)) continue;
			if ($post['likes']['count'] < $likes_min) continue;
			
			//if (!isset($post['attachment'])) continue;
			//if ($post['attachment']['type'] !== 'photo') continue;
			// проверить на рекламу
			if ($commerical == 'true') {
				if (isset($post['copy_owner_id'])) {
					continue;
				}
				if (preg_match('/\[club(.*)\]*/i', $post['text'])) {
					continue;
				}
				if (preg_match('/(vk\.|vkontakte\.)/i', $post['text'])) {
					continue;
				}
				if (preg_match('/(http)/i', $post['text'])) {
					continue;
				}
			}
			$select = PostPeer::getInstance()->select()->where('external_id = ?', $post['id']);
			$row = PostPeer::getInstance()->fetchRow($select);
			if ($row) {
				continue;
			}
			
			$img = isset($post['attachment']['photo']['src_big']) ? $post['attachment']['photo']['src_big'] : null;
			$result[$post['id']] = array(
				'text' => $post['text'],
				'likes' => $post['likes']['count'],
				'date' => date('Y-m-d H:i:s', $post['date']),
				'img' => $img,
				'tag' => $group[0]['name']
			);
		}
		
		$this->view->posts = $result;
    }
}
